﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Timers;
using ScreenMonkey.Core;
using ScreenMonkey.Clips;
using ScreenMonkey.Clips.Extensions;
using ScreenMonkey.Plugin.Clips.Basic.Text;

namespace ScreenMonkey.Plugin.Clips.SMHelloWorld
{
    public class CountdownTextClip : TextClip, ICustomPaint, ISetupScreen, IMenuSettings, ICueClip 
    {
        public Dictionary<string, TimeSpan> CountdownSpans;
        public Dictionary<string, TextItem> PlainTextItems;
        public DateTime startTime { get; set; }

        public override string TypeName
        {
            get { return "Countdown"; }
        }

        public override string Caption
        {
            get { return "Countdown"; }
        }

        public override string Description
        {
            get { return "Countdown text"; }
        }

        [Obsolete]
        protected override void OnInitialiseShow()
        {
            base.OnInitialiseShow();
            CountdownSpans = new Dictionary<string, TimeSpan>();
            PlainTextItems = new Dictionary<string, TextItem>();
            TimeSpan countdownSpan;
            foreach (TextItem item in Items.Values)
            {
                if (TimeSpan.TryParse(item.Text, out countdownSpan))
                {
                    item.Text = "";
                    CountdownSpans.Add(item.Name, countdownSpan);
                }
                else
                {
                    PlainTextItems.Add(item.Name, item);
                }
            }
            System.Timers.Timer aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = 1000;
            aTimer.Enabled = true;
            startTime = DateTime.Now;
        }

        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            bool needRefresh = true;
            foreach (TextItem item in Items.Values)
            {
                if (needRefresh)
                {
                    needRefresh = false;
                    
                }
            }
        }

        public override void StopShow()
        {
            foreach (KeyValuePair<string, TimeSpan> kvp in CountdownSpans)
            {
                Items[kvp.Key].Text = kvp.Value.ToString();
            }
            CountdownSpans.Clear();
            base.StopShow();
        }

        new public void Paint(Bitmap canvas, Rectangle area, Rectangle panArea)
        {
            base.Paint(canvas, area, panArea);
            Graphics gCanvas = Graphics.FromImage(canvas);
            gCanvas.CompositingMode = CompositingMode.SourceCopy;
            gCanvas.FillRectangle(new SolidBrush(Color.Transparent), area);
            gCanvas.CompositingMode = CompositingMode.SourceOver;
            gCanvas.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
            TimeSpan timePassed = DateTime.Now.Subtract(startTime);
            timePassed = TimeSpan.FromSeconds(Math.Round(timePassed.TotalSeconds, 0));
                
            foreach (var displayStyle in DisplayStyle.TextItems) 
            {   
                string itemName = displayStyle.Name;
                TimeSpan countdownSpan;
                string displayString;
                if (CountdownSpans.TryGetValue(itemName, out countdownSpan))
                {
                    TimeSpan countdownSpanLeft = CountdownSpans[itemName].Subtract(timePassed);
                    displayString = countdownSpanLeft.ToString();
                }
                else
                {
                    displayString = PlainTextItems[itemName].Text;
                }
                SolidBrush sb = new SolidBrush(displayStyle.ForeColour);
                gCanvas.DrawString(displayString, displayStyle.Font, sb, displayStyle.Margin.ResizeRectangle(area), GetStringFormatFromContentAlignment(displayStyle.Alignment));
            }
        }

        private StringFormat GetStringFormatFromContentAlignment(ContentAlignment ca)
        {
            StringFormat format = new StringFormat();
            switch (ca)
            {
                case ContentAlignment.TopCenter:
                    format.Alignment = StringAlignment.Near;
                    format.LineAlignment = StringAlignment.Center;
                    break;
                case ContentAlignment.TopLeft:
                    format.Alignment = StringAlignment.Near;
                    format.LineAlignment = StringAlignment.Near;
                    break;
                case ContentAlignment.TopRight:
                    format.Alignment = StringAlignment.Near;
                    format.LineAlignment = StringAlignment.Far;
                    break;
                case ContentAlignment.MiddleCenter:
                    format.Alignment = StringAlignment.Center;
                    format.LineAlignment = StringAlignment.Center;
                    break;
                case ContentAlignment.MiddleLeft:
                    format.Alignment = StringAlignment.Center;
                    format.LineAlignment = StringAlignment.Near;
                    break;
                case ContentAlignment.MiddleRight:
                    format.Alignment = StringAlignment.Center;
                    format.LineAlignment = StringAlignment.Far;
                    break;
                case ContentAlignment.BottomCenter:
                    format.Alignment = StringAlignment.Far;
                    format.LineAlignment = StringAlignment.Center;
                    break;
                case ContentAlignment.BottomLeft:
                    format.Alignment = StringAlignment.Far;
                    format.LineAlignment = StringAlignment.Near;
                    break;
                case ContentAlignment.BottomRight:
                    format.Alignment = StringAlignment.Far;
                    format.LineAlignment = StringAlignment.Far;
                    break;
            }
            return format;
        }

    }
}
